import { createRouter, createWebHashHistory } from 'vue-router'
import Home from '../views/Home.vue'
import store from '../store'
const routes = [
    {
        path: '/login',
        name: 'login',
        meta: {
            title: 'XINHUANXIANG'
        },
        component: () => import('../views/login.vue')
    },
    {
        path: '/',
        name: 'home',
        meta: {
            title: 'XINHUANXIANG'
        },
        component: () => import('../views/home/index.vue'),
        children: [
            {
                path: '/',
                name: 'index',
                meta: {
                    title: 'XINHUANXIANG'
                },
                component: () => import('../views/home/index/index.vue')
            }
        ]
    },
    {
        path: '/search',
        name: 'search',
        meta: {
            title: '搜索'
        },
        component: () => import('../views/home/search/index.vue')
    },
    {
        path: '/detail',
        name: 'detail',
        meta: {
            title: 'XINHUANXIANG'
        },
        component: () => import('../views/home/detail/index.vue')
    },{
        path: '/category',
        name: 'category',
        meta: {
            title: 'XINHUANXIANG'
        },
        component: () => import('../views/home/category/index.vue')
    },
    {
        path: '/person',
        name: 'person',
        meta: {
            title: '首页'
        },
        component: () => import('../views/person/index.vue'),
        children: [
            {
                path: '/person/center',
                name: 'person_center',
                meta: {
                    title: ' '
                },
                component: () => import('../views/person/center/index.vue')
            },{
                path: '/person/changepwd',
                name: 'person_changepwd',
                meta: {
                    title: '修改手机号码'
                },
                component: () => import('../views/person/changePwd/index.vue')
            },{
                path: '/person/browsingHistory',
                name: 'person_browsingHistory',
                meta: {
                    title: '浏览记录'
                },
                component: () => import('../views/person/browsingHistory/index.vue')
            },{
                path: '/person/course',
                name: 'person_course',
                meta: {
                    title: '我的课程'
                },
                component: () => import('../views/person/course/index.vue')
            },{
                path: '/person/balance',
                name: 'person_balance',
                meta: {
                    title: '我的余额'
                },
                component: () => import('../views/person/balance/index.vue')
            },{
                path: '/course',
                name: 'course',
                meta: {
                    title: '我的课程'
                },
                component: () => import('../views/home/course/index.vue')
            }
        ]
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})


const history = window.sessionStorage
history.clear();
let historyCount: any = history.getItem('count') || 0
history.setItem('/', '0')

let isPush = false

let isTouchStart = false
let endTime = Date.now()
let methods = ['push', 'go', 'replace', 'forward', 'back']

document.addEventListener('touchend', () => {
    isTouchStart = false
    endTime = Date.now()
})
document.addEventListener('touchstart', () => {
    isTouchStart = true
})
methods.forEach(key => {
    let method = router[key].bind(router)
    router[key] = function (...args: any) {
        isPush = true
        method.apply(null, args)
    }
})
router.beforeEach(function (to, from, next) {
    document.title = to.meta.title
    setTimeout(function () { //加延时使上面的popstate先走  哈希模式 则不需要加延时
        store.commit('updateLoadingStatus', { isLoading: true })

        const toIndex = history.getItem(to.path)
        const fromIndex = history.getItem(from.path)
        let direction

        if (toIndex) {
            if (!fromIndex || parseInt(toIndex, 10) > parseInt(fromIndex, 10) || (toIndex === '0' && fromIndex === '0')) {
                direction = 'forward'
            } else {
                direction = 'reverse'
            }
        } else {
            ++historyCount
            history.setItem('count', historyCount)
            to.path !== '/' && history.setItem(to.path, historyCount)
            direction = 'forward'
        }

        // 判断是否是ios左滑返回 或者 右滑前进
        if (toIndex && toIndex !== '0' && !isPush && (((Date.now() - endTime) < 377) || isTouchStart)) {
            direction = ''
        }
        store.commit('updateDirection', { direction })
        isTouchStart = false
        next()

    }, 0)
})
router.afterEach(function (to) {
    isPush = false
    store.commit('updateLoadingStatus', { isLoading: false })
})
export default router
